package com.zhuss.food.processor;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.food.order.wechat.CreateTopicBo;
import com.zhuss.bean.bo.food.order.wechat.TopicOrderBo;
import com.zhuss.bean.vo.food.order.DetailOrderVo;
import com.zhuss.bean.vo.food.product.DetailProductVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.WpsException;
import com.zhuss.food.db.dao.ext.OrderProductDao;
import com.zhuss.food.db.dao.ext.TopicDao;
import com.zhuss.food.db.dao.ext.TopicProductDao;
import com.zhuss.food.db.entity.OrderProduct;
import com.zhuss.food.db.entity.Topic;
import com.zhuss.food.db.entity.TopicProduct;
import com.zhuss.util.JsonUtil;


@Component("topicCreate")
public class TopicCreate extends AbstractProcessor<Object, CreateTopicBo> implements Processor<Object, CreateTopicBo> {
	
	@Autowired
	private TopicDao topicDao;
	
	@Autowired
	private TopicProductDao topicProductDao;
	
	@Autowired
	@Qualifier("orderRead")
	private OrderRead orderRead;
	
	@Autowired
	@Qualifier("productRead")
	private ProductRead productRead;
	
	@Autowired
	private OrderProductDao orderProductDao;
	
	@Autowired
	@Qualifier("orderTopic")
	private Processor<Object, TopicOrderBo> orderTopic;

	@Override
	public Object process(CreateTopicBo bo) throws WpsException {
		
		Long orderId = bo.getOrderId();
		
		TopicOrderBo orderBo = new TopicOrderBo();
		BeanUtils.copyProperties(bo, orderBo);
		orderBo.setOrderId(orderId);
		orderTopic.process(orderBo);
		
		DetailOrderVo order = (DetailOrderVo) orderRead.detail(orderId);
		Topic topic = new Topic();
		topic.setContent(bo.getContent());
		topic.setLevel(bo.getLevel());
		topic.setOrderNo(order.getOrderNo());
		topic.setOrderId(orderId);
		
		topic.setCreatorId(bo.getConcurrentUserId());
		topic.setCreator(bo.getConcurrentUserName());
		topic.setCreateTime(new Date());
		topicDao.create(topic);
		
		
		List<OrderProduct> orderProductList = orderProductDao.listOrderProduct(order.getOrderId());
		for (OrderProduct item : orderProductList) {
			Long productId = item.getProductId();
			DetailProductVo product = (DetailProductVo) productRead.detail(productId);
			Long foodId = product.getFoodId();
			TopicProduct topicProduct = new TopicProduct();
			topicProduct.setFoodNo(product.getFoodNo());
			topicProduct.setFoodId(foodId);
			topicProduct.setProductNo(product.getProductNo());
			topicProduct.setProductId(productId);
			topicProduct.setTopicId(topic.getTopicId());
			
			topicProduct.setCreatorId(bo.getConcurrentUserId());
			topicProduct.setCreator(bo.getConcurrentUserName());
			topicProduct.setCreateTime(new Date());
			topicProductDao.create(topicProduct);
		}
		
		
		return null;
	}

	@Override
	public Object log(CreateTopicBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.餐饮.key());
    	record.setAction(LogActionEnum.评价创建.key());
    	record.setTargetId(param.getTargetId());
    	record.setTargetName(param.getOrderId() + ", 等级：" + param.getLevel() + ", 内容" + param.getContent());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}

	@Override
	public void check(CreateTopicBo param) throws WpsException {
		// TODO Auto-generated method stub
		
	}

}
